package com.wsj.a.collapsingdemo;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.wsj.a.collapsingdemo.callback.EmptyCallback;
import com.wsj.a.collapsingdemo.callback.ErrorCallback;
import com.wsj.a.collapsingdemo.callback.LoadingCallback;
import com.wsj.a.collapsingdemo.databinding.ActivityWaterFall2Binding;
import com.wsj.a.collapsingdemo.databinding.ItemWaterFallBinding;
import com.wsj.a.collapsingdemo.entity.WorkEntity;
import com.wsj.a.collapsingdemo.widget.recycler.recycler.RecyclerViewContract;
import com.wsj.a.collapsingdemo.widget.recycler.recycler.RecyclerViewDelegate;
import com.wsj.a.collapsingdemo.widget.recycler.recycler.StateFulListener;
import com.wsj.library.loadsir.callback.Callback;
import com.wsj.library.loadsir.core.LoadService;
import com.wsj.library.loadsir.core.LoadSir;

import java.util.ArrayList;
import java.util.List;

public class WaterFallActivity2 extends AppCompatActivity implements RecyclerViewContract.IFAdapter<WorkEntity>,
        RecyclerViewContract.IFLoadData,
        StateFulListener {

    private ActivityWaterFall2Binding mBinding;
    //加载布局
    protected LoadService mLoadService;
    private RecyclerViewDelegate<WorkEntity> mRecyclerViewDelegate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_water_fall2);
        mBinding.includeBaseTitle.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initLoadSir();
        initRecyclerView();
    }



    //加载失败显示占位图
    private void initLoadSir() {
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new EmptyCallback())
                .addCallback(new ErrorCallback())
                .addCallback(new LoadingCallback())
                .build();

        mLoadService = loadSir.register(mBinding.contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mRecyclerViewDelegate.reLoadData();
            }
        });
    }


    public void initRecyclerView(){
        mRecyclerViewDelegate = RecyclerViewDelegate.with(this, this)
                .StaggeredGridRecyclerView(mBinding.refreshView, mBinding.recyclerView)
                .isShowLoadingView(false)
                .cancelRefresh(false,true)
                .stateFulListener(this)
                .build();
        mRecyclerViewDelegate.reLoadData();
    }

    //显示空布局
    @Override
    public void showEmptyView() {
        mLoadService.showCallback(EmptyCallback.class);
    }

    //显示错误布局
    @Override
    public void showErrorView() {
        mLoadService.showCallback(ErrorCallback.class);
    }

    //显示内容布局
    @Override
    public void showContentView() {
        mLoadService.showSuccess();
    }

    //上拉刷新
    @Override
    public void onRefresh() {
        getDataInfo();
    }

    //加载更多
    @Override
    public void onLoadMore() {
        getDataInfo();
    }

    @Override
    public void updateView(@NonNull WorkEntity data, @NonNull ViewDataBinding binding, int position) {
        ItemWaterFallBinding itemWaterFallBinding = (ItemWaterFallBinding) binding;
        itemWaterFallBinding.nickname.setText(data.nickname);
        itemWaterFallBinding.image.getLayoutParams().height = data.imageHeight;
        itemWaterFallBinding.image.setImageResource(R.drawable.img_2);
    }

    @Override
    public ViewDataBinding createView(ViewGroup parent, int type) {
        return DataBindingUtil.inflate(getLayoutInflater(),R.layout.item_water_fall,null,false);
    }

    public void getDataInfo(){
        List<WorkEntity> mDatas = new ArrayList<>();
        if(mRecyclerViewDelegate.getDataList() != null && mRecyclerViewDelegate.getDataSize() > 0){
            mDatas.addAll(mRecyclerViewDelegate.getDataList());
        }
        mDatas.addAll(initData());
        mRecyclerViewDelegate.render(mDatas);
    }

    public List<WorkEntity> initData(){
        List<WorkEntity> mData = new ArrayList<>();

        String [] nicknames = new String[]{"范冰冰","赵薇","袁泉","赵丽颖","李冰冰","舒淇","黄圣依","刘亦菲","曹颖","杨幂"};
        String [] headImages = new String[]{"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788329728&di=051c7504b400b838376aa7eec2a66bb5&imgtype=0&src=http%3A%2F%2Fs15.sinaimg.cn%2Fmw690%2F001PrNfEgy71DVOVBgife%26690",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788395118&di=84a61ec3df0f80878f22cd24c02d1f92&imgtype=0&src=http%3A%2F%2Fp.cdn.sohu.com%2Fbc4e413e%2Fe1e4b530152dc4cdd6ec340dffaac618.jpeg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788448601&di=d55977be14613dda0d07a510243368f4&imgtype=0&src=http%3A%2F%2Fapp.why.com.cn%2Fepaper%2Fshzk%2Fimages%2F2014-06%2F24%2FA19%2Fa19p19_b.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788495763&di=a634f95181f7c1be9cf09937f3c64080&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201404%2F28%2F20140428222525_heZZX.jpeg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788530823&di=def5c2fb972a5c32d948758a96eea2e9&imgtype=0&src=http%3A%2F%2Fimg3.cache.netease.com%2Fphoto%2F0026%2F2014-08-18%2FA3UNS1N525A20026.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788556296&di=01fda63499bb9483406a90aaf473ce5d&imgtype=0&src=http%3A%2F%2Fimg5q.duitang.com%2Fuploads%2Fitem%2F201502%2F06%2F20150206084438_xYejE.thumb.700_0.jpeg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788615036&di=604eaea7865e2041bc319a876b385096&imgtype=0&src=http%3A%2F%2Fimg01.jixiexinxi5.com%2Fupload%2F201701%2F22%2F135905901.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788644700&di=64ce46b5a786ce16a66757347c14a101&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201501%2F09%2F20150109093459_KmVrx.jpeg",
                "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1100322184,3942798567&fm=11&gp=0.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788714713&di=c9ecf2fc30e490bb62d0bb20d4f1dd64&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201510%2F08%2F20151008112536_GVRwJ.jpeg"};

        for(int i=0;i<10;i++) {
            WorkEntity workEntity = new WorkEntity();
            workEntity.headImage = headImages[i];
            workEntity.nickname = nicknames[i];
            workEntity.imageHeight = (i % 2) * 100 + 400; //偶数和奇数的图片设置不同的高度，以到达错开的目的
            mData.add(workEntity);
        }
        return mData;
    }
}
